﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using YBApproval.Model;

namespace YBApproval
{
    public class ControlHelper
    {
        public static TableCell[] ForTextBox(Field field, string value="")
        {
            TableCell lbCell = ForLabel(field);

            TableCell cell = new TableCell();
            if (field.IsWholeRow)
            {
                cell.ColumnSpan = 3;
            }
            TextBox tb = new TextBox();
            tb.ID = field.Id.ToString();
            tb.Text = value;
            cell.Controls.Add(tb);
            return new TableCell[] { lbCell, cell };
        }        

        public static TableCell[] ForDecimal(Field field, string value="")
        {
            TableCell lbCell = ForLabel(field);

            TableCell cell = new TableCell();
            if (field.IsWholeRow)
            {
                cell.ColumnSpan = 3;
            }
            TextBox tb = new TextBox();
            tb.ID = field.Id.ToString();
            tb.Text = value;
            tb.CssClass = "decimal";
            cell.Controls.Add(tb);
            return new TableCell[] { lbCell, cell };
        }

        public static TableCell[] ForInteger(Field field, string value = "")
        {
            TableCell lbCell = ForLabel(field);

            TableCell cell = new TableCell();
            if (field.IsWholeRow)
            {
                cell.ColumnSpan = 3;
            }
            TextBox tb = new TextBox();
            tb.ID = field.Id.ToString();
            tb.Text = value;
            tb.CssClass = "integer";
            cell.Controls.Add(tb);
            return new TableCell[] { lbCell, cell };
        }        

        public static TableCell[] ForDropdownList(Field field,string value="")
        {
            TableCell lbCell = ForLabel(field);

            TableCell cell = new TableCell();
            if (field.IsWholeRow)
            {
                cell.ColumnSpan = 3;
            }
            DropDownList dpl = new DropDownList();
            foreach (var item in field.OptionList.OptionItems)
            {
                ListItem li = new ListItem(item.Text, item.Value);
                if (li.Value == value)
                {
                    li.Selected = true;
                }
                dpl.Items.Add(li);
            }
            dpl.Items.Insert(0, new ListItem("请选择", ""));
            dpl.ID = field.Id.ToString();
            cell.Controls.Add(dpl);
            return new TableCell[] { lbCell, cell };
        }        

        public static TableCell[] ForRadioList(Field field, string value = "")
        {
            TableCell lbCell = ForLabel(field);

            RadioButtonList list = new RadioButtonList();
            list.ID = field.Id.ToString();
            list.RepeatDirection = RepeatDirection.Horizontal;
            foreach (var item in field.OptionList.OptionItems)
            {
                ListItem listItem = new ListItem(item.Text, item.Value);
                if (listItem.Value == value)
                {
                    listItem.Selected = true;
                }
                list.Items.Add(listItem);
            }
            TableCell cell = new TableCell();
            if (field.IsWholeRow)
            {
                cell.ColumnSpan = 3;
            }
            cell.Controls.Add(list);
            return new TableCell[] { lbCell, cell };
        }
        
        public static TableCell[] ForCheckBoxList(Field field, string value = "")
        {
            TableCell lbCell = ForLabel(field);

            CheckBoxList list = new CheckBoxList();
            list.ValidationGroup = field.Id.ToString();
            list.ID = field.Id.ToString();
            list.RepeatDirection = RepeatDirection.Horizontal;
            string[] values = value.Split(',');
            foreach (var item in field.OptionList.OptionItems)
            {
                ListItem listItem = new ListItem(item.Text, item.Value);
                if (values.Contains(item.Value))
                {
                    listItem.Selected = true;
                }
                list.Items.Add(listItem);
            }
            TableCell cell = new TableCell();
            if (field.IsWholeRow)
            {
                cell.ColumnSpan = 3;
            }
            cell.Controls.Add(list);
            return new TableCell[] { lbCell, cell };
        }



        public static TableCell[] ForDatePicker(Field field, string value = "")
        {
            TableCell lbCell = ForLabel(field);

            TableCell cell = new TableCell();
            if (field.IsWholeRow)
            {
                cell.ColumnSpan = 3;
            }
            TextBox tb = new TextBox();
            tb.ID = field.Id.ToString();
            tb.Text = string.Format("{0:yyyy-MM-dd}", value);
            tb.CssClass = "datepicker";
            cell.Controls.Add(tb);
            return new TableCell[] { lbCell, cell };
        }



        public static TableCell[] ForTextarea(Field field, string value = "")
        {
            TableCell lbCell = ForLabel(field);

            TableCell cell = new TableCell();
            if (field.IsWholeRow)
            {
                cell.ColumnSpan = 3;
            }
            TextBox tb = new TextBox();
            tb.ID = field.Id.ToString();
            tb.Text = value;
            tb.TextMode = TextBoxMode.MultiLine;
            tb.Height = 80;
            tb.Width = 260;
            cell.Controls.Add(tb);
            return new TableCell[] { lbCell, cell };
        }

        public static TableCell[] ForView(InstanceValue instValue)
        {
            TableCell nameCell = new TableCell();
            nameCell.Text = instValue.Field.Name + ":";
            TableCell valueCell = new TableCell();
            if (instValue.Field.IsWholeRow)
            {
                valueCell.ColumnSpan = 3;
            }
            valueCell.Text = instValue.Text;
            return new TableCell[] { nameCell, valueCell };
        }

        public static TableCell ForLabel(Field field)
        {
            TableCell lbCell = new TableCell();
            Label lb = new Label();
            lb.Text = field.Name + ":";
            lbCell.Controls.Add(lb);

            if (field.IsRequired)
            {
                Label label = new Label();
                label.Text = "*";
                label.CssClass = "required";
                lbCell.Controls.Add(label);
            }
            return lbCell;
        }

    }

   
}